package de.muehle;


import java.awt.Color;
import java.awt.Component;
import java.util.Vector;

import javax.media.j3d.*;
import javax.swing.JPanel;
import javax.vecmath.*;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;

public class BoxHaus extends Shape3D {
	private Geometry geometry = null; //TODO
	private Appearance appearance = null;
	private float widthBase;
	private float widthGipfel;
	private float height;
	static private final double hectKons =(1+2*Math.cos(Math.PI/4));;
	
	public BoxHaus(float width1, float width2, float height)
	{
		super();
		widthBase = width1;
		widthGipfel = width2;
		this.height = height;
		geometry = createGeometry();
		appearance = createAppearance();
		this.setGeometry(geometry);
		this.setAppearance(appearance);
	}

	public BoxHaus(){
		super();
		widthBase = 3f;
		widthGipfel = 1f;
		height = 4f;
		geometry = createGeometry();
		appearance = createAppearance();
		this.setGeometry(geometry);
		this.setAppearance(appearance);
	}

	private Appearance createAppearance() {
		return defineAppearance();
	}

	private Geometry createGeometry() {
		IndexedQuadArray pyramidGeometry = new IndexedQuadArray(16, IndexedQuadArray.COORDINATES|IndexedQuadArray.TEXTURE_COORDINATE_2, 32);
		pyramidGeometry.setCoordinates(0, getKoordinaten());
		pyramidGeometry.setCoordinateIndices(0, getIndices());
		TexCoord2f[] texCoords= {	new TexCoord2f(1.0f,1.0f),
				new TexCoord2f(0.0f,1.0f),
				new TexCoord2f(0.0f,0.0f),
				new TexCoord2f(1.0f,0.0f)};
		pyramidGeometry.setTextureCoordinates(0, 0, texCoords);
		pyramidGeometry.setTextureCoordinateIndices(0,0,getTextIndices() );

		return pyramidGeometry;
	}

	private int[] getTextIndices() { 
		int[] indices = new int[8*4];		
		for(int i = 0; i < 8; i++)
		{
			// Polygone de base
			indices[4*i] = i;
			indices[4*i+1] = (i+1)%8;
			
			// Polygone haut
			indices[4*i+2] =8+((i+1)%8);
			indices[4*i+3] = 8+(i);
		}

		return indices;
	}

	protected Appearance defineAppearance() {
		  TextureLoader loader = new TextureLoader("textures/Holz_Streifen.jpg", null); 
          Texture2D tex = (Texture2D)loader.getTexture(); 
          Appearance app = new Appearance(); 
          app.setTexture(tex); 
          app.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0f)); 
		return app;
	}

	private int[] getIndices() {
		int[] indices = new int[8*4];
		for(int i = 0; i < 8; i++)
		{
			// Polygone de base
			indices[4*i] = i;
			indices[4*i+1] = (i+1)%8;
			
			// Liaison entre les deux polygones
			indices[4*i+2] = 8+(i+1)%8;
			indices[4*i+3] = 8+i;
		}
		return indices;
	}
	
	private  Point3f[] getKoordinaten() {
		Vector<Point3f> allPoints1 = getVectorPoly(widthBase, 0);
		Vector<Point3f> allPoints2 = getVectorPoly(widthGipfel, height);
		Vector<Point3f> allPoints = new Vector<Point3f>();
		allPoints.addAll(allPoints1);
		allPoints.addAll(allPoints2);
		Point3f[] array = new Point3f[16];
		allPoints.toArray(array );
		return array;
	}

	static public Vector<Point3f> getVectorPoly(float base, float yHeight) {
		float a = (float) (base/hectKons);
		Vector<Point3f> allPoints = new Vector<Point3f>();
		allPoints.add(new Point3f(-a/2, yHeight, -base/2));//1
		allPoints.add(new Point3f(a/2, yHeight, -base/2));//2
		allPoints.add(new Point3f(base/2, yHeight, -a/2));//3
		allPoints.add(new Point3f(base/2, yHeight, a/2));//4
		allPoints.add(new Point3f(a/2, yHeight, base/2));//5
		allPoints.add(new Point3f(-a/2, yHeight, base/2));//6
		allPoints.add(new Point3f(-base/2, yHeight, a/2));//7
		allPoints.add(new Point3f(-base/2, yHeight, -a/2));//8
		return allPoints;
	}
}